package fr.mywiki.view.web.struts.action.version;

import java.rmi.RemoteException;

import javax.ejb.FinderException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import fr.mywiki.business.entry.EntryLite;
import fr.mywiki.business.versioning.VersionLite;
import fr.mywiki.business.versioning.VersionManager;
import fr.mywiki.model.ejbAdapter.DataController;
import fr.mywiki.view.web.struts.form.version.VersionListForm;

/**
 * Manages the display of a list of versions objects.
 * 
 * @author tlombard
 */
public class VersionListAction extends DispatchAction {
	/**
	 * The list of versions to display is contained in a request attribute whose
	 * name is <code>LIST_VERSIONS</code>.
	 */
	public static final String LIST_VERSIONS = "list_versions";

	/**
	 * The entry whose list of versions is required is given by this request
	 * parameter, which contains its <code>Long</code> id.
	 */
	public static final String ENTRY_ID = "versionId";
	
	/**
	 * The list of versions to display is contained in a request attribute whose
	 * name is <code>LIST_VERSIONS</code>.
	 */
	public static final String VERSION_ID = "versionId";
	
	/**
	 * Displays a list of versions. A list of <code>VersionLite</code> objects
	 * is passed through the request.
	 */
	public ActionForward display(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse arg3) throws RemoteException {
		Long entryId = new Long(request.getAttribute(ENTRY_ID).toString());
		EntryLite entry = (EntryLite) DataController.getValue(entryId,
				EntryLite.TYPE_ENT);
		
		((VersionListForm)actionForm).initVersionsList(entry);
		
		return mapping.findForward("display");
	}
	
	/**
	 * Displays a specified version.
	 */
	public ActionForward displayVersion(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse arg3) {
		VersionListForm form = (VersionListForm) actionForm;
		Long versionId = new Long(form.getVersionId());
		request.setAttribute(ENTRY_ID, versionId);
		return mapping.findForward("displayEntry");
	}
	
	/**
	 * The selected version becomes the new current version for the entry. Then
	 * the entry is displayed with the current version.
	 */
	public ActionForward rollback(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse arg3) throws RemoteException, FinderException {
		VersionListForm form = (VersionListForm) actionForm;
		Long versionId = new Long(form.getVersionId());
		VersionManager.getInstance().setCurrentVersion((VersionLite)
				DataController.getValue(versionId, VersionLite.TYPE_ENT));
		request.setAttribute(ENTRY_ID, versionId);
		return mapping.findForward("displayEntry");
	}
	
	/**
	 * The two selected versions are to be compared.
	 */
	public ActionForward compare(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse arg3) {
		VersionListForm form = (VersionListForm) actionForm;
		request.setAttribute(CompareVersionsAction.FROM_VERSION,
				new Long(form.getFrom()));
		request.setAttribute(CompareVersionsAction.TO_VERSION,
				new Long(form.getTo()));
		return mapping.findForward("compareVersions");
	} 
}